import { createWebHashHistory, createRouter } from 'vue-router';
import Layout from '../Layout/index.vue';
import LoginContainer from '../modules/user/login/index.vue';
import AdvanceForm from '../modules/AdvancedForm/AdvancedForm.vue';

const routes = [
  {
    path: '/user/login',
    name: 'LoginContainer',
    component: LoginContainer
  },
  {
    path: '/',
    name: 'LayoutContainer',
    component: Layout,
    redirect: '/advanced-form',
    children: [
      {
        path: 'advanced-form',
        name: 'AdvanceForm',
        component: AdvanceForm
      },
      {
        path:'/example/student',
        name:'StudentForm',
        component: ()=>import('../modules/student/Student.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
});

router.beforeEach((to, from, next) => {
  if (to.path === '/user/login') {
    next();
  } else {
    let info = sessionStorage.getItem('user_info');
    if (info && info !== '{}') {
      next();
    } else {
      next('/user/login')
    }
  }
})

export default router;